package com.example.moonshine.redchange.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.moonshine.redchange.Data.ChageData;
import com.example.moonshine.redchange.Data.QData;
import com.example.moonshine.redchange.Data.QueryData;
import com.example.moonshine.redchange.R;
import com.example.moonshine.redchange.Util.DataUrl;
import com.example.moonshine.redchange.Util.SureDialog;
import com.github.zackratos.ultimatebar.UltimateBar;
import com.google.gson.Gson;
import com.zyao89.view.zloading.ZLoadingDialog;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.content.ContentValues.TAG;
import static com.zyao89.view.zloading.Z_TYPE.CIRCLE;

/**
 * Created by moonshine on 2018/4/16.
 */

@SuppressLint("Registered")
public class changeDataActivity extends Activity {

    TextView realtime_left;
    TextView realtime_right;

    Button gua_left_add;
    Button gua_left_less;
    TextView gua_left_data;
    Button gua_right_add;
    Button gua_right_less;
    TextView gua_right_data;

    Button gang_left_add;
    Button gang_left_less;
    TextView gang_left_data;
    Button gang_right_add;
    Button gang_right_less;
    TextView gang_right_data;


    Button jiao_left_add;
    Button jiao_left_less;
    TextView jiao_left_data;
    Button jiao_right_add;
    Button jiao_right_less;
    TextView jiao_right_data;


    public  static final int SUCCESS = 1;
    public  static final int ERROR = 2;


    TextView real_time_left;
    TextView real_time_right;
    String values;
    String  status;
    Button button;
    List<QData> listdata;
    private LocalReceiver mReceiver;
    ZLoadingDialog dialog = new ZLoadingDialog(changeDataActivity.this);
    @SuppressLint("HandlerLeak")

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            if (message.what ==SUCCESS){
                gang_left_data.setText("0");
                gang_right_data.setText("0");
                gua_left_data.setText("0");
                gua_right_data.setText("0");
                jiao_left_data.setText("0");
                jiao_right_data.setText("0");
                Toast.makeText(changeDataActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
            }else if (message.what ==SUCCESS){
                Toast.makeText(changeDataActivity.this,"提交失败",Toast.LENGTH_SHORT).show();
            }
        }
    };

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_changedata);
        UltimateBar.newColorBuilder()
                .statusColor(Color.parseColor("#0a3640"))       // 状态栏颜色
                .statusDepth(50)                // 状态栏颜色深度
                .build(this)
                .apply();

            initView();

//        dataChange();
//        init();
        button = findViewById(R.id.btn);
        TextView textView = findViewById(R.id.title_text_tv);
        final Intent intent = getIntent();
        values = intent.getStringExtra("values");
        status = intent.getStringExtra("status");

        if (values.equals("1")) {
            realtime_left.setText("二号操作侧测量值:");
            realtime_right.setText("二号传动侧测量值:");
            textView.setText("底漆湿膜("+status+")");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                if (gua_right_data.getText().toString().equals("0") && gang_left_data.getText().toString().equals("0")
                        && gang_right_data.getText().toString().equals("0") && gua_left_data.getText().toString().equals("0")){
                    Toast.makeText(changeDataActivity.this,"您没有修改任何数据",Toast.LENGTH_SHORT).show();
                }else {
                    showDialog();
                }
                        //子线程请求数据

                }
            });
        } else if (values.equals("2")) {
            realtime_left.setText("六号操作侧测量值:");
            realtime_right.setText("六号传动侧测量值:");
            textView.setText("背漆湿膜("+status+")");

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (gua_right_data.getText().toString().equals("0") && gang_left_data.getText().toString().equals("0")
                            && gang_right_data.getText().toString().equals("0") && gua_left_data.getText().toString().equals("0")){
                        Toast.makeText(changeDataActivity.this,"您没有修改任何数据",Toast.LENGTH_SHORT).show();
                    }else {
                        showDialog();
                    }
                    //子线程请求数据

                }
            });
        } else if (values.equals("3"))
        {
            textView.setText("面漆湿膜("+status+")");
            realtime_left.setText("四号操作侧测量值:");
            realtime_right.setText("四号传动侧测量值:");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (gua_right_data.getText().toString().equals("0") && gang_left_data.getText().toString().equals("0")
                            && gang_right_data.getText().toString().equals("0") && gua_left_data.getText().toString().equals("0")){
                        Toast.makeText(changeDataActivity.this,"您没有修改任何数据",Toast.LENGTH_SHORT).show();
                    }else {
                        showDialog();
                    }
                    //子线程请求数据

//                        face_shi_left = changeleft.getText().toString();
//                        face_shi_right = changeright.getText().toString();
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Looper.prepare();
//                                try {
//                                    String result = postChangeDateHttp(order_id, data_pack_time, plate_thickness, plate_width, plate_length,
//                                            bottom_dry_thickness, bottom_dry_thickness_d, bottom_dry_thickness_d_l, bottom_dry_thickness_d_r,
//                                            bottom_wet_thickness, bottom_wet_thickness_d, bottom_wet_thickness_d_l, bottom_wet_thickness_d_r,
//                                            back_dry_thickness, back_dry_thickness_d, back_dry_thickness_d_l, back_dry_thickness_d_r,
//                                            back_wet_thickness, back_wet_thickness_d, back_wet_thickness_d_l, back_wet_thickness_d_r,
//                                            surface_dry_thickness, surface_dry_thickness_d, surface_dry_thickness_d_l, surface_dry_thickness_d_r,
//                                            surface_wet_thickness, surface_wet_thickness_d, face_shi_left, face_shi_right,
//                                            temperature_1_1, temperature_1_1_d, temperature_1_2, temperature_1_2_d, temperature_1_3, temperature_1_3_d, temperature_1_4, temperature_1_4_d, temperature_1_5, temperature_1_5_d,
//                                            temperature_2_1, temperature_2_1_d, temperature_2_2, temperature_2_2_d, temperature_2_3, temperature_2_3_d, temperature_2_4, temperature_2_4_d, temperature_2_5, temperature_2_5_d,
//                                            exhaust_air_volume_1, exhaust_air_volume_1_d, exhaust_air_volume_2, exhaust_air_volume_2_d);
//                                    if (result.equals("10")) {
//                                        dialog.dismiss();
//                                        Toast.makeText(getApplicationContext(), "提交成功", Toast.LENGTH_SHORT).show();
//                                        finish();
//                                    } else {
//                                        dialog.dismiss();
//                                        Toast.makeText(getApplicationContext(), "提交失败，服务器故障,请重试", Toast.LENGTH_SHORT).show();
//                                    }
//                                } catch (Exception e) {
//                                    dialog.dismiss();
//                                    e.printStackTrace();
//                                    Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
//                                }
//                                Looper.loop();
//                            }
//                        }).start();
                }
            });
        } else if (values.equals("4")) {

            textView.setText("底漆干膜("+status+")");
            realtime_left.setText("三号操作侧测量值:");
            realtime_right.setText("三号传动侧测量值:");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (gua_right_data.getText().toString().equals("0") && gang_left_data.getText().toString().equals("0")
                            && gang_right_data.getText().toString().equals("0") && gua_left_data.getText().toString().equals("0")){
                        Toast.makeText(changeDataActivity.this,"您没有修改任何数据",Toast.LENGTH_SHORT).show();
                    }else {
                        showDialog();
                    }
                    //子线程请求数据

//                        bottom_gan_lefshowDialog();
////                        bottom_gan_left = changeleft.getText().toString();
//                        bottom_gan_right = changeright.getText().toString();
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Looper.prepare();
//                                try {
//                                    String result = postChangeDateHttp(order_id, data_pack_time, plate_thickness, plate_width, plate_length,
//                                            bottom_dry_thickness, bottom_dry_thickness_d, bottom_gan_left, bottom_gan_right,
//                                            bottom_wet_thickness, bottom_wet_thickness_d, bottom_wet_thickness_d_l, bottom_wet_thickness_d_r,
//                                            back_dry_thickness, back_dry_thickness_d, back_dry_thickness_d_l, back_dry_thickness_d_r,
//                                            back_wet_thickness, back_wet_thickness_d, back_wet_thickness_d_l, back_wet_thickness_d_r,
//                                            surface_dry_thickness, surface_dry_thickness_d, surface_dry_thickness_d_l, surface_dry_thickness_d_r,
//                                            surface_wet_thickness, surface_wet_thickness_d, surface_wet_thickness_d_l, surface_wet_thickness_d_r,
//                                            temperature_1_1, temperature_1_1_d, temperature_1_2, temperature_1_2_d, temperature_1_3, temperature_1_3_d, temperature_1_4, temperature_1_4_d, temperature_1_5, temperature_1_5_d,
//                                            temperature_2_1, temperature_2_1_d, temperature_2_2, temperature_2_2_d, temperature_2_3, temperature_2_3_d, temperature_2_4, temperature_2_4_d, temperature_2_5, temperature_2_5_d,
//                                            exhaust_air_volume_1, exhaust_air_volume_1_d, exhaust_air_volume_2, exhaust_air_volume_2_d);
//                                    if (result.equals("10")) {
//                                        dialog.dismiss();
//                                        Toast.makeText(getApplicationContext(), "提交成功", Toast.LENGTH_SHORT).show();
//                                        finish();
//                                    } else {
//                                        dialog.dismiss();
//                                        Toast.makeText(getApplicationContext(), "提交失败，服务器故障,请重试", Toast.LENGTH_SHORT).show();
//                                    }
//
//                                } catch (Exception e) {
//                                    dialog.dismiss();
//                                    e.printStackTrace();
//                                    Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
//                                }
//                                Looper.loop();
//                            }
//                        }).start();
                }
            });
        } else if (values.equals("5")) {
            textView.setText("背漆干膜("+status+")");
            realtime_left.setText("六号操作侧测量值:");
            realtime_right.setText("六号传动侧测量值:");

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (gua_right_data.getText().toString().equals("0") && gang_left_data.getText().toString().equals("0")
                            && gang_right_data.getText().toString().equals("0") && gua_left_data.getText().toString().equals("0")){
                        Toast.makeText(changeDataActivity.this,"您没有修改任何数据",Toast.LENGTH_SHORT).show();
                    }else {
                        showDialog();
                    }
                    //子线程请求数据


//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Looper.prepare();
//                                try {
//                                    String result = postChangeDateHttp(order_id, data_pack_time, plate_thickness, plate_width, plate_length,
//                                            bottom_dry_thickness, bottom_dry_thickness_d, bottom_dry_thickness_d_l, bottom_dry_thickness_d_r,
//                                            bottom_wet_thickness, bottom_wet_thickness_d, bottom_wet_thickness_d_l, bottom_wet_thickness_d_r,
//                                            back_dry_thickness, back_dry_thickness_d, back_gan_left, back_gan_right,
//                                            back_wet_thickness, back_wet_thickness_d, back_wet_thickness_d_l, back_wet_thickness_d_r,
//                                            surface_dry_thickness, surface_dry_thickness_d, surface_dry_thickness_d_l, surface_dry_thickness_d_r,
//                                            surface_wet_thickness, surface_wet_thickness_d, surface_wet_thickness_d_l, surface_wet_thickness_d_r,
//                                            temperature_1_1, temperature_1_1_d, temperature_1_2, temperature_1_2_d, temperature_1_3, temperature_1_3_d, temperature_1_4, temperature_1_4_d, temperature_1_5, temperature_1_5_d,
//                                            temperature_2_1, temperature_2_1_d, temperature_2_2, temperature_2_2_d, temperature_2_3, temperature_2_3_d, temperature_2_4, temperature_2_4_d, temperature_2_5, temperature_2_5_d,
//                                            exhaust_air_volume_1, exhaust_air_volume_1_d, exhaust_air_volume_2, exhaust_air_volume_2_d);
//                                    if (result.equals("10")) {
//                                        dialog.dismiss();
//                                        Toast.makeText(getApplicationContext(), "提交成功", Toast.LENGTH_SHORT).show();
//                                        finish();
//                                    } else {
//                                        dialog.dismiss();
//                                        Toast.makeText(getApplicationContext(), "提交失败，服务器故障,请重试", Toast.LENGTH_SHORT).show();
//                                    }
//                                } catch (Exception e) {
//                                    dialog.dismiss();
//                                    e.printStackTrace();
//                                    Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
//                                }
//                                Looper.loop();
//                            }
//                        }).start();

                }
            });
        } else {
            textView.setText("面漆干膜("+status+")");
            realtime_left.setText("六号操作侧测量值:");
            realtime_right.setText("六号传动侧测量值:");
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (gua_right_data.getText().toString().equals("0") && gang_left_data.getText().toString().equals("0")
                            && gang_right_data.getText().toString().equals("0") && gua_left_data.getText().toString().equals("0")){
                        Toast.makeText(changeDataActivity.this,"您没有修改任何数据",Toast.LENGTH_SHORT).show();
                    }else {
                        showDialog();
                    }
                    //子线程请求数据

                }
            });
        }
    }

//    public String postChangeDateHttp(String order_id, String data_pack_time, String plate_thickness, String plate_width, String plate_length,
//                                     String bottom_dry_thickness, String bottom_dry_thickness_d, String bottom_dry_thickness_d_l, String bottom_dry_thickness_d_r,
//                                     String bottom_wet_thickness, String bottom_wet_thickness_d, String bottom_wet_thickness_d_l, String bottom_wet_thickness_d_r,
//                                     String back_dry_thickness, String back_dry_thickness_d, String back_dry_thickness_d_l, String back_dry_thickness_d_r,
//                                     String back_wet_thickness, String back_wet_thickness_d, String back_wet_thickness_d_l, String back_wet_thickness_d_r,
//                                     String surface_dry_thickness, String surface_dry_thickness_d, String surface_dry_thickness_d_l, String surface_dry_thickness_d_r,
//                                     String surface_wet_thickness, String surface_wet_thickness_d, String surfacewet_thickness_d_l, String surface_wet_thickness_d_r,
//                                     String temperature_1_1, String temperature_1_1_d, String temperature_1_2, String temperature_1_2_d, String temperature_1_3, String temperature_1_3_d,
//                                     String temperature_1_4, String temperature_1_4_d, String temperature_1_5, String temperature_1_5_d, String temperature_2_1, String temperature_2_1_d,
//                                     String temperature_2_2, String temperature_2_2_d, String temperature_2_3, String temperature_2_3_d, String temperature_2_4, String temperature_2_4_d,
//                                     String temperature_2_5, String temperature_2_5_d, String exhaust_air_volume_1, String exhaust_air_volume_1_d, String exhaust_air_volume_2, String exhaust_air_volume_2_d) {
//        String status1 = "";
////        String changeDataUrl = "http://1785s28l17.iask.in:19568/element-admin/technics/product-data-alert";
//        String changeDataUrl = "http://119.23.219.22:80/element-admin/technics/product-data-alert";
//
//        OkHttpClient client = new OkHttpClient().newBuilder()
//                .retryOnConnectionFailure(false)
//                .readTimeout(20, TimeUnit.SECONDS)
//                .connectTimeout(20, TimeUnit.SECONDS)
//                .build();
//        Gson gson = new Gson();
////        String data ="{\"back_dry_thickness\":\"0.0000\",\"back_dry_thickness_d\":\"200\",\"back_dry_thickness_d_l\":\"150\",\"back_dry_thickness_d_r\":\"150\",\"back_wet_thickness\":\"0.0000\",\"back_wet_thickness_d\":\"400\",\"back_wet_thickness_d_l\":\"\",\"back_wet_thickness_d_r\":\"\",\"bottom_dry_thickness\":\"0.0000\",\"bottom_dry_thickness_d\":\"120\",\"bottom_dry_thickness_d_l\":\"130\",\"bottom_dry_thickness_d_r\":\"160\",\"bottom_wet_thickness\":\"0.0000\",\"bottom_wet_thickness_d\":\"200\",\\n\"bottom_wet_thickness_d_l\":\"100\",\"bottom_wet_thickness_d_r\":\"100\",\"data_pack_time\":\"1536123048\",\"exhaust_air_volume_1\":\"4000\",\"exhaust_air_volume_1_d\":\"10\",\"exhaust_air_volume_2\":\"10000\",\"exhaust_air_volume_2_d\":\"10\",\"order_id\":\"15361162803425\",\"plate_length\":\"1042702.6875\",\"plate_thickness\":\"900.0000\",\"plate_width\":\"120.0000\",\"surface_dry_thickness\":\"0.0000\",\"surface_dry_thickness_d\":\"200\",\"surface_dry_thickness_d_l\":\"200\",\"surface_dry_thickness_d_r\":\"200\",\"surface_wet_thickness\":\"0.0000\",\"surface_wet_thickness_d\":\"400\",\"surface_wet_thickness_d_l\":\"200\",\"surface_wet_thickness_d_r\":\"150\",\"temperature_1_1\":\"149\",\"temperature_1_1_d\":\"20\",\"temperature_1_2\":\"200\",\"temperature_1_2_d\":\"0\",\"temperature_1_3\":\"280\",\"temperature_1_3_d\":\"15\",\"temperature_1_4\":\"240\",\"temperature_1_4_d\":\"4\",\"temperature_1_5\":\"149\",\"temperature_1_5_d\":\"6\",\"temperature_2_1\":\"210\",\"temperature_2_1_d\":\"20\",\"temperature_2_2\":\"254\",\"temperature_2_2_d\":\"13\",\"temperature_2_3\":\"290\",\"temperature_2_3_d\":\"7\"}";
//        ChangedataData data = new ChangedataData(order_id, data_pack_time, plate_thickness, plate_width, plate_length,
//                bottom_dry_thickness, bottom_dry_thickness_d, bottom_dry_thickness_d_l, bottom_dry_thickness_d_r,
//                bottom_wet_thickness, bottom_wet_thickness_d, bottom_wet_thickness_d_l, bottom_wet_thickness_d_r,
//                back_dry_thickness, back_dry_thickness_d, back_dry_thickness_d_l, back_dry_thickness_d_r,
//                back_wet_thickness, back_wet_thickness_d, back_wet_thickness_d_l, back_wet_thickness_d_r,
//                surface_dry_thickness, surface_dry_thickness_d, surface_dry_thickness_d_l, surface_dry_thickness_d_r,
//                surface_wet_thickness, surface_wet_thickness_d, surfacewet_thickness_d_l, surface_wet_thickness_d_r,
//                temperature_1_1, temperature_1_1_d, temperature_1_2, temperature_1_2_d, temperature_1_3, temperature_1_3_d,
//                temperature_1_4, temperature_1_4_d, temperature_1_5, temperature_1_5_d, temperature_2_1, temperature_2_1_d,
//                temperature_2_2, temperature_2_2_d, temperature_2_3, temperature_2_3_d, temperature_2_4, temperature_2_4_d,
//                temperature_2_5, temperature_2_5_d, exhaust_air_volume_1, exhaust_air_volume_1_d, exhaust_air_volume_2, exhaust_air_volume_2_d);
//        String json = gson.toJson(data);
//        Log.i("changeDataActivity", "json" + json);
//        MediaType JSON = MediaType.parse("application/json;charset=utf-8");
//        RequestBody requestBody = FormBody.create(JSON, json);
//        Request request = new Request.Builder()
//                .header("Transfer-Encoding", "chunked")
//                .url(changeDataUrl)
//                .post(requestBody)
//                .build();
//        try {
//            Response response = client.newCall(request).execute();
//            String result = response.body().string();
//            ChangeDate changeDate = gson.fromJson(result, ChangeDate.class);
//            status1 = changeDate.getStatus();
//            Log.i("changeDataActivity", "返回值为" + result);
//        } catch (Exception e) {
//            e.printStackTrace();
//            Toast.makeText(changeDataActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
//        }
//        return status1;
//    }

//    public void runview() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                new AnotherTask().execute("");
//            }
//        }).start();
//    }

//    @SuppressLint("StaticFieldLeak")
//    private class AnotherTask extends AsyncTask<String, Void, String> {
//        @Override
//        protected void onPostExecute(String result) {
//            //对UI组件的更新操作
//            Gson gson = new Gson();
//            try {
////                ChangeDate changeData = gson.fromJson(result, ChangeDate.class);
////                ChangedataData changedataData = changeData.getData();
////                back_dry_thickness = changedataData.getBack_dry_thickness();
////                back_dry_thickness_d = changedataData.getBack_dry_thickness_d();
////                back_dry_thickness_d_l = changedataData.getBack_dry_thickness_d_l();
////                back_dry_thickness_d_r = changedataData.getBack_dry_thickness_d_r();
////                back_wet_thickness = changedataData.getBack_wet_thickness();
////                back_wet_thickness_d = changedataData.getBack_wet_thickness_d();
////                back_wet_thickness_d_l = changedataData.getBack_wet_thickness_d_l();
////                back_wet_thickness_d_r = changedataData.getBack_wet_thickness_d_r();
////                bottom_dry_thickness = changedataData.getBottom_dry_thickness();
////                bottom_dry_thickness_d = changedataData.getBottom_dry_thickness_d();
////                bottom_dry_thickness_d_l = changedataData.getBottom_dry_thickness_d_l();
////                bottom_dry_thickness_d_r = changedataData.getBottom_dry_thickness_d_r();
////                bottom_wet_thickness = changedataData.getBottom_wet_thickness();
////                bottom_wet_thickness_d = changedataData.getBottom_wet_thickness_d();
////                bottom_wet_thickness_d_l = changedataData.getBottom_wet_thickness_d_l();
////                bottom_wet_thickness_d_r = changedataData.getBottom_wet_thickness_d_r();
////                surface_dry_thickness = changedataData.getSurface_dry_thickness();
////                surface_dry_thickness_d = changedataData.getSurface_dry_thickness_d();
////                surface_dry_thickness_d_l = changedataData.getSurface_dry_thickness_d_l();
////                surface_dry_thickness_d_r = changedataData.getSurface_dry_thickness_d_r();
////                surface_dry_thickness_d_l = changedataData.getSurface_dry_thickness_d_l();
////                surface_wet_thickness = changedataData.getSurface_wet_thickness();
////                surface_wet_thickness_d = changedataData.getSurface_wet_thickness_d();
////                surface_wet_thickness_d_l = changedataData.getSurface_wet_thickness_d_l();
////                surface_wet_thickness_d_r = changedataData.getSurface_wet_thickness_d_r();
////                surface_dry_thickness_d_r = changedataData.getSurface_dry_thickness_d_r();
////
////                data_pack_time = changedataData.getData_pack_time();
////                plate_length = changedataData.getPlate_length();
////                plate_thickness = changedataData.getPlate_thickness();
////                plate_width = changedataData.getPlate_width();
////
////                temperature_1_1 = changedataData.getTemperature_1_1();
////                temperature_1_1_d = changedataData.getTemperature_1_1_d();
////                temperature_1_2 = changedataData.getTemperature_1_2();
////                temperature_1_2_d = changedataData.getTemperature_1_2_d();
////                temperature_1_3 = changedataData.getTemperature_1_3();
////                temperature_1_3_d = changedataData.getTemperature_1_3_d();
////                temperature_1_4 = changedataData.getTemperature_1_4();
////                temperature_1_4_d = changedataData.getTemperature_1_4_d();
////                temperature_1_5 = changedataData.getTemperature_1_5();
////                temperature_1_5_d = changedataData.getTemperature_1_5_d();
////                temperature_2_1 = changedataData.getTemperature_2_1();
////                temperature_2_1_d = changedataData.getTemperature_2_1_d();
////                temperature_2_2 = changedataData.getTemperature_2_2();
////                temperature_2_2_d = changedataData.getTemperature_2_2_d();
////                temperature_2_3 = changedataData.getTemperature_2_3();
////                temperature_2_3_d = changedataData.getTemperature_2_3_d();
////                temperature_2_4 = changedataData.getTemperature_2_4();
////                temperature_2_4_d = changedataData.getTemperature_2_4_d();
////                temperature_2_5 = changedataData.getTemperature_2_5();
////                temperature_2_5_d = changedataData.getTemperature_2_5_d();
////
////                exhaust_air_volume_1 = changedataData.getExhaust_air_volume_1();
////                exhaust_air_volume_1_d = changedataData.getExhaust_air_volume_1_d();
////                exhaust_air_volume_2 = changedataData.getExhaust_air_volume_2();
////                exhaust_air_volume_2_d = changedataData.getExhaust_air_volume_2_d();
//
//
//                if (values.equals("1")) {
//                    tx1.setText(bottom_wet_thickness);
//                    tx2.setText(bottom_wet_thickness_d);
//                    tx3.setText(bottom_wet_thickness_d_l);
//                    tx4.setText(bottom_wet_thickness_d_r);
//
//                } else if (values.equals("5")) {
//                    tx1.setText(back_dry_thickness);
//                    tx2.setText(back_dry_thickness_d);
//                    tx3.setText(back_dry_thickness_d_l);
//                    tx4.setText(back_dry_thickness_d_r);
//
//
//                } else if (values.equals("3")) {
//                    tx1.setText(surface_wet_thickness);
//                    tx2.setText(surface_wet_thickness_d);
//                    tx3.setText(surface_wet_thickness_d_l);
//                    tx4.setText(surface_wet_thickness_d_r);
//
//
//                } else if (values.equals("4")) {
//                    tx1.setText(bottom_dry_thickness);
//                    tx2.setText(bottom_dry_thickness_d);
//                    tx3.setText(bottom_dry_thickness_d_l);
//                    tx4.setText(bottom_dry_thickness_d_r);
//
//                } else if (values.equals("2")) {
//                    tx1.setText(back_wet_thickness);
//                    tx2.setText(back_wet_thickness_d);
//                    tx3.setText(back_wet_thickness_d_l);
//                    tx4.setText(back_wet_thickness_d_r);
//
//                } else {
//
//                    tx1.setText(surface_dry_thickness);
//                    tx2.setText(surface_dry_thickness_d);
//                    tx3.setText(surface_dry_thickness_d_l);
//                    tx4.setText(surface_dry_thickness_d_r);
//
//                }
//
//            } catch (JsonSyntaxException e) {
//                e.printStackTrace();
//                Toast.makeText(changeDataActivity.this, "数据刷新错误", Toast.LENGTH_LONG).show();
//            }
//
//
//        }
//
//        @Override
//        protected String doInBackground(String... params) {
//            //耗时的操作
//            OkHttpClient client = new OkHttpClient();
//            Gson gson = new Gson();
//            OidData data = new OidData();
//            data.setOid(selectOid());
//            String json = gson.toJson(data);
//            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
//            RequestBody requestBody = FormBody.create(JSON, json);
//            Request request = new Request.Builder()
//                    .url(new DataUrl().changeDataActivityUrl)
//                    .post(requestBody)
//                    .build();
//            String result = null;
//            try {
//                Response response = client.newCall(request).execute();
//                result = response.body().string();
//
//            } catch (IOException e) {
//                e.printStackTrace();
//                Log.e(TAG, "doInBackground: ", e);
//            }
//            return result;
//        }
//
//    }

//    public String selectOid() {
//        DataDBHepler dataDBHepler = new DataDBHepler(getBaseContext());
//        ArrayList<SelectOid> DataList = dataDBHepler.FindOidData();
//        final SelectOid data = new SelectOid(DataList.get(0).getId(), DataList.get(0).getOid());
//        Log.i(TAG, "数据库的Oid为：" + data.getOid());
//        final String Oid = data.getOid();
//        return Oid;
//    }

    public void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                new AnotherTask1().execute("");
            }
        }).start();
    }
    //刷新实时监控的测量值
    private class AnotherTask1 extends AsyncTask<String, Void, String> {
        @SuppressLint("SetTextI18n")
        @Override
        protected void onPostExecute(String result) {
            //对UI组件的更新操作
            Gson gson = new Gson();
            try {
                QueryData queryData = gson.fromJson(result, QueryData.class);
                 listdata = queryData.getValues();
                 Log.i("tag","数字据"+listdata.size());
                DecimalFormat df = new DecimalFormat("#.####");
                switch (values) {
                    case "1":
                        real_time_left.setText(df.format(listdata.get(2).getsData()) + "毫米");
//
                        real_time_right.setText(df.format(listdata.get(3).getsData()) + "毫米");
                        break;
                    case "2":
                        real_time_left.setText(df.format(listdata.get(8).getsData()) + "毫米");
                        real_time_right.setText(df.format(listdata.get(9).getsData()) + "毫米");
                        break;
                    case "3":
                        real_time_left.setText(df.format(listdata.get(6).getsData()) + "毫米");
                        real_time_right.setText(df.format(listdata.get(7).getsData()) + "毫米");
                        break;
                    case "4":
                        real_time_left.setText(df.format(listdata.get(8).getsData()) + "毫米");
                        real_time_right.setText(df.format(listdata.get(9).getsData()) + "毫米");
                        break;
                    case "5":
                        real_time_left.setText(df.format(listdata.get(8).getsData()) + "毫米");
                        real_time_right.setText(df.format(listdata.get(9).getsData()) + "毫米");
                        break;
                    case "6":
                        real_time_left.setText(df.format(listdata.get(8).getsData()) + "毫米");
                        real_time_right.setText(df.format(listdata.get(9).getsData()) + "毫米");
                        break;
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        @Override
        protected String doInBackground(String... params) {
            //耗时的操作

            OkHttpClient client = new OkHttpClient();
            Gson gson = new Gson();
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            FormBody body = new FormBody.Builder()
                    .build();
            Request request = new Request.Builder()
                    .url(new DataUrl().changeDataActivityAuditedUrl)
                    .post(body)
                    .build();
            String result ="";
            try {
                Response response = client.newCall(request).execute();
                //获取后台传输的额status状态码

                result = response.body().string();

            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "doInBackground: ",e );
            }
            return result;

        }
    }

    //更新实时测量数据
    public void dataChange(){
       final Handler  handler1 = new Handler();
      Runnable  runnable1 = new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                //要做的事情
                init();
                handler1.postDelayed(this, 1000);
            }
        };
        handler1.postDelayed(runnable1, 1000);
    }


    public String posthttpresult(String json,String path) {

        //okhttp Post请求传输Json数据
        String result = null;
        OkHttpClient client = new OkHttpClient();

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = FormBody.create(JSON, json);//放进requestBoday中
        Request request = new Request.Builder()
                .url(path)
                .post(requestBody)
                .build();

        try {
            Response response = client.newCall(request).execute();

            //获取后台传输的额status状态码
           result = response.body().string();

        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(changeDataActivity.this,"服务器故障",Toast.LENGTH_LONG).show();
        }
        return result;
    }


//
//    /**
//     * @param keyCode
//     * @param event   监听手机back键 点击返回界面
//     * @return
//     */
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
//            //启动一个意图,回到桌面
//            handler1.removeCallbacks(runnable1);
//        }
//        return super.onKeyDown(keyCode, event);
//    }


    //上传数据
    public void setData (final String value){
        dialog.setLoadingBuilder(CIRCLE)//设置类型
                .setLoadingColor(Color.GREEN)//颜色
                .setHintText("上传中...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.GREEN)  // 设置字体颜色
                .setDurationTime(0.5) // 设置动画时间百分比 - 0.5倍
//                        .setDialogBackgroundColor(Color.parseColor("#CC111111")) // 设置背景色，默认白色
                .setCanceledOnTouchOutside(false)
                .show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                List<ChageData> dateList = new ArrayList<>();
                if (value.equals("1") ||value.equals("2")){
                    String name = "一涂操作侧刮刀辊";
                    int value = Integer.parseInt(String.valueOf(gua_left_data.getText()));
                    ChageData date = new ChageData(name,value);
                    String name1 = "一涂传动侧刮刀辊";
                    int value1 = Integer.parseInt(String.valueOf(gua_right_data.getText()));
                    ChageData date1 = new ChageData(name1,value1);
                    String name2 = "一涂操作侧钢辊";
                    int value2 = Integer.parseInt(String.valueOf(gang_left_data.getText()));
                    ChageData date2 = new ChageData(name2,value2);
                    String name3 = "一涂传动侧钢辊";
                    int value3= Integer.parseInt(String.valueOf(gang_right_data.getText()));
                    ChageData date3 = new ChageData(name3,value3);
                    String name4 = "一涂操作侧胶辊";
                    int value4= Integer.parseInt(String.valueOf(jiao_left_data.getText()));
                    ChageData date4 = new ChageData(name4,value4);
                    String name5 = "一涂传动侧胶辊";
                    int value5= Integer.parseInt(String.valueOf(jiao_right_data.getText()));
                    ChageData date5 = new ChageData(name5,value5);
                    dateList.add(date);
                    dateList.add(date1);
                    dateList.add(date2);
                    dateList.add(date3);
                    dateList.add(date4);
                    dateList.add(date5);
                }else {
                    String name = "二涂操作侧刮刀辊";
                    int value = Integer.parseInt(String.valueOf(gua_left_data.getText()));
                    ChageData date = new ChageData(name,value);
                    String name1 = "二涂传动侧刮刀辊";
                    int value1 = Integer.parseInt(String.valueOf(gua_right_data.getText()));
                    ChageData date1 = new ChageData(name1,value1);
                    String name2 = "二涂操作侧钢辊";
                    int value2 = Integer.parseInt(String.valueOf(gang_left_data.getText()));
                    ChageData date2 = new ChageData(name2,value2);
                    String name3 = "二涂传动侧钢辊";
                    int value3= Integer.parseInt(String.valueOf(gang_right_data.getText()));
                    ChageData date3 = new ChageData(name3,value3);
                    String name4 = "二涂操作侧胶辊";
                    int value4= Integer.parseInt(String.valueOf(jiao_right_data.getText()));
                    ChageData date4 = new ChageData(name4,value4);
                    String name5 = "二涂传动侧胶辊";
                    int value5= Integer.parseInt(String.valueOf(jiao_right_data.getText()));
                    ChageData date5 = new ChageData(name5,value5);
                    dateList.add(date);
                    dateList.add(date1);
                    dateList.add(date2);
                    dateList.add(date3);
                    dateList.add(date4);
                    dateList.add(date5);
                }
                Gson gson = new Gson();
                String json = gson.toJson(dateList);//将其转换为JSON数据格式
                try {
                    String result = posthttpresult(json,DataUrl.changedataUrl);
                    if (result.equals("200")){
                        dialog.dismiss();
                        Toast.makeText(changeDataActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
                        Message message = new Message();
                        message.what = SUCCESS;
                        handler.sendMessage(message);
                    }else {
                        dialog.dismiss();
                        Message message = new Message();
                        message.what = ERROR;
                        handler.sendMessage(message);
                        Toast.makeText(changeDataActivity.this,"提交失败",Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dialog.dismiss();
                    Toast.makeText(changeDataActivity.this,"提交失败，请求超时",Toast.LENGTH_SHORT).show();

                }
                Looper.loop();
            }
        }).start();

    }


   //展示警告提示框
    public void showDialog() {
        final SureDialog Dialog = new SureDialog(changeDataActivity.this);
        Dialog.setTitle("消息提示");
        Dialog.setMessage("您已修改参数，是否确认提交?");
        Dialog.setYesOnclickListener("确定", new SureDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                setData(values);
                Dialog.dismiss();
            }
        });
        Dialog.setNoOnclickListener("取消", new SureDialog.onNoOnclickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onNoClick() {
                Dialog.dismiss();
            }
        });
        Dialog.show();

    }

    @Override
    protected void onResume() {
        super.onResume();

        //发送广播给service
        Intent intent = new Intent();
        intent.putExtra("status", "change_home");
        intent.setAction("service");
        BroadCastManager.getInstance().sendBroadCast(this, intent);

        gua_left_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gua_left_data.getText()));
                String result = String.valueOf(data + i);
                gua_left_data.setText(result);
            }
        });
        gua_left_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gua_left_data.getText()));
                String result = String.valueOf(data - i);
                gua_left_data.setText(result);
            }
        });

        gua_right_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gua_right_data.getText()));
                String result = String.valueOf(data + i);
                gua_right_data.setText(result);
            }
        });
        gua_right_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gua_right_data.getText()));
                String result = String.valueOf(data - i);
                gua_right_data.setText(result);
            }
        });



        gang_left_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gang_left_data.getText()));
                String result = String.valueOf(data + i);
                gang_left_data.setText(result);
            }
        });
        gang_left_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gang_left_data.getText()));
                String result = String.valueOf(data - i);
                gang_left_data.setText(result);
            }
        });

        gang_right_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gang_right_data.getText()));
                String result = String.valueOf(data + i);
                gang_right_data.setText(result);
            }
        });
        gang_right_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(gang_right_data.getText()));
                String result = String.valueOf(data - i);
                gang_right_data.setText(result);
            }
        });


        jiao_left_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(jiao_left_data.getText()));
                String result = String.valueOf(data + i);
                jiao_left_data.setText(result);
            }
        });
        jiao_left_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(jiao_left_data.getText()));
                String result = String.valueOf(data - i);
                jiao_left_data.setText(result);
            }
        });

        jiao_right_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(jiao_right_data.getText()));
                String result = String.valueOf(data + i);
                jiao_right_data.setText(result);
            }
        });
        jiao_right_less.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = 1;
                int data = Integer.parseInt(String.valueOf(jiao_right_data.getText()));
                String result = String.valueOf(data - i);
                jiao_right_data.setText(result);
            }
        });


        try {
            IntentFilter filter = new IntentFilter();
            filter.addAction("change_home");
            mReceiver = new LocalReceiver();
            BroadCastManager.getInstance().registerReceiver(changeDataActivity.this, mReceiver, filter);//注册广播接收者
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void initView(){
        realtime_left = findViewById(R.id.realtime_left);
        realtime_right = findViewById(R.id.realtime_right);
        real_time_left = findViewById(R.id.real_time_left);
        real_time_right = findViewById(R.id.real_time_right);

        gua_left_add = findViewById(R.id.gua_left_add);
        gua_left_less = findViewById(R.id.gua_left_less);
        gua_left_data = findViewById(R.id.gua_left_data);

        gua_right_add = findViewById(R.id.gua_right_add);
        gua_right_less = findViewById(R.id.gua_right_less);
        gua_right_data = findViewById(R.id.gua_right_data);

        gang_left_add = findViewById(R.id.gang_left_add);
        gang_left_less = findViewById(R.id.gang_left_less);
        gang_left_data = findViewById(R.id.gang_left_data);

        gang_right_add = findViewById(R.id.gang_right_add);
        gang_right_less = findViewById(R.id.gang_right_less);
        gang_right_data = findViewById(R.id.gang_right_data);

        jiao_left_add = findViewById(R.id.jiao_left_add);
        jiao_left_less = findViewById(R.id.jiao_left_less);
        jiao_left_data = findViewById(R.id.jiao_left_data);

        jiao_right_add = findViewById(R.id.jiao_right_add);
        jiao_right_less = findViewById(R.id.jiao_right_less);
        jiao_right_data = findViewById(R.id.jiao_right_data);
    }


    //提示警告
    @SuppressLint("ResourceAsColor")
    public void showNoProject(String msg){
        TextView title = new TextView(this);
        title.setText("警告");
        title.setGravity(Gravity.CENTER);
        title.setTextSize(20);
        android.support.v7.app.AlertDialog builder = new android.support.v7.app.AlertDialog.Builder(this)
                .setMessage(msg)
                .setCustomTitle(title)
                .create();
        builder.show();
    }


    //收到广播后处理
    class LocalReceiver extends BroadcastReceiver {

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onReceive(Context context, Intent intent) {
            //收到广播后的处理
            String text = intent.getStringExtra("text");
            try {
                if (text.equals("111")){
                    showNoProject("一涂左侧厚度过小");
                }else if (text.equals("112")){
                    showNoProject("一涂左侧厚度过大");
                }else if (text.equals("122")){
                    showNoProject("一涂右侧厚度过大");
                }else if (text.equals("121")){
                    showNoProject("一涂右侧厚度过小");
                }else if (text.equals("211")){
                    showNoProject("二涂左侧厚度过小");
                }else if (text.equals("212")){
                    showNoProject("二涂左侧厚度过大");
                }else if (text.equals("222")){
                    showNoProject("二涂右侧厚度过大");
                }else if (text.equals("221")){
                    showNoProject("二涂右侧厚度过小");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.i("tag", "传参的数字为:" + text);
        }
    }
}
